package com.allen.admin.config;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author
 */
@Configuration
@ComponentScan
@Import(value = {})
public class RootConfig {

    private static final Logger logger = LoggerFactory.getLogger(RootConfig.class);

    @Autowired
    private Environment env;


    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(4);
        taskExecutor.setMaxPoolSize(8);
        taskExecutor.setQueueCapacity(16);
        return taskExecutor;
    }

//    @Bean
//    public RedisConnectionFactory redisConnectionFactory() {
//        List<String> redisNodes = redisNodes();
//        RedisClusterConfiguration redisClusterConfig = new RedisClusterConfiguration(redisNodes);
//        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfig);
//        jedisConnectionFactory.setTimeout(2000 * 5);
//        return jedisConnectionFactory;
//    }
//
//    @Bean("redisTemplate")
//    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
//    @Bean("stringRedisTemplate")
//    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        return new StringRedisTemplate(redisConnectionFactory);
//    }
//
//    @Bean("shiroRedisTemplate")
//    public RedisTemplate<?, ?> shiroRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }


    private List<String> redisNodes() {
        String redisNodesStr = env.getRequiredProperty("hs.admin.redis.server.list");
        logger.info("Redis nodes [{}]", redisNodesStr);
        String[] nodeArr = StringUtils.split(redisNodesStr, ',');
        if (nodeArr != null) {
            return Arrays.asList(nodeArr);
        } else {
            return new ArrayList<>();
        }
    }

}
